package com.xisoft.base.config;

import cn.hutool.db.ds.AbstractDSFactory;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.bee.BeeDSFactory;
import cn.hutool.db.ds.c3p0.C3p0DSFactory;
import cn.hutool.db.ds.dbcp.DbcpDSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import cn.hutool.db.ds.hikari.HikariDSFactory;
import cn.hutool.db.ds.pooled.PooledDSFactory;
import cn.hutool.db.ds.simple.SimpleDSFactory;
import cn.hutool.db.ds.tomcat.TomcatDSFactory;
import cn.hutool.setting.Setting;
import com.xisoft.common.enums.DataPoolType;
import com.xisoft.common.pubEnv.JudgeAssertUtil;
import com.xisoft.common.pubEnv.PubEnvUtil;
import com.xisoft.common.pubEnv.StringUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Created with Description:
 * 功能描述:  〈druid 配置多数据源〉
 *
 * @fileRelativePath: DruidConfig.java
 * @Date: Created on 2022/7/26  13:32  <br>
 * @author: LiBencheng <br>
 * @version: 1.0 <br>
 */
@Configuration
public class DataSrcConfig
{
    public String POOLTYPE = "poolType";
    public String user = "******";
    public String group = "";
    public DSFactory dsFactory = null;

    @Bean
    public DataSource createDruidDs( )
    {
        // int i=0/0;
        Setting setting = new Setting("config/xisotft.setting", true);
        AtomicReference< Boolean > enable = new AtomicReference<>(Boolean.FALSE);
        setting.getGroupedMap().keySet().stream()
               .filter(key -> !PubEnvUtil.isEmpty(key))
               .forEach(key ->
                        {
                            JudgeAssertUtil.checkAssert(PubEnvUtil.equals(StringUtil.valueOfZero(enable.get())
                                                                , setting.getGroupedMap().get(key).get("enable"))
                                    , "数据源文件[config/db .setting]，里只能有一个启动的数据源！！！");
                            if (PubEnvUtil.equals("true", setting.getGroupedMap().get(key).get("enable")))
                            {
                                group = key;
                                enable.set(Boolean.TRUE);
                                LinkedHashMap< String,String > srcMap = setting.getGroupedMap().get(key);
                                srcMap.remove("enable");
                                setting.getGroupedMap().get("").put("url", srcMap.get("url"));
                                setting.getGroupedMap().get("").put("user", srcMap.get("user"));
                                setting.getGroupedMap().get("").put("pass", srcMap.get("pass"));
                                setting.getGroupedMap().get("").put("remarks", srcMap.get("remarks"));
                            }
                        });
        user = setting.get("", "user");
        dsFactory = DSFactory.setCurrentDSFactory(getDsFactory(setting));

        return dsFactory.getDataSource();
    }

    /************************************************************* <br>
     *
     *说明: 抽象数据源工厂<br>
     *
     * @Description   <br>
     * @Date Created on 2022/7/27  14:35  <br>
     * @param setting <br>
     * @return cn.hutool.db.ds.AbstractDSFactory  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    private AbstractDSFactory getDsFactory(Setting setting)
    {
        String pooltype = setting.get(POOLTYPE);
        AbstractDSFactory dsFactory = null;
        if (PubEnvUtil.equals(DataPoolType.BEE.code, pooltype))
        { dsFactory = new BeeDSFactory(setting); }
        else if (PubEnvUtil.equals(DataPoolType.C3P0.code, pooltype))
        { dsFactory = new C3p0DSFactory(setting); }
        else if (PubEnvUtil.equals(DataPoolType.DBCP.code, pooltype))
        { dsFactory = new DbcpDSFactory(setting); }
        else if (PubEnvUtil.equals(DataPoolType.DRUID.code, pooltype))
        { dsFactory = new DruidDSFactory(setting); }
        else if (PubEnvUtil.equals(DataPoolType.HIKARI.code, pooltype))
        { dsFactory = new HikariDSFactory(setting); }
        else if (PubEnvUtil.equals(DataPoolType.TOMCAT.code, pooltype))
        { dsFactory = new TomcatDSFactory(setting); }
        else if (PubEnvUtil.equals(DataPoolType.POOLED.code, pooltype))
        { dsFactory = new PooledDSFactory(setting); }
        else { dsFactory = new SimpleDSFactory(setting); }

        return dsFactory;
    }
}
